local skel = fk.CreateSkill {
  name = "emo__jiaoliu",
}

Fk:loadTranslationTable{
  ["emo__jiaoliu"] = "交流",
  [":emo__jiaoliu"] = "出牌阶段限一次，或你受到伤害后，你可以与一名其他角色同时交换一张手牌，你以此法获得的牌不计入手牌上限，且若此牌为：伤害牌，此牌伤害基数+1；非伤害牌，你与其各摸一张牌。",

  ["#emo__jiaoliu-prompt"] = "交流：选择一张手牌和一名其他角色，用此牌交换其一张手牌",
  ["#emo__jiaoliu-card"] = "交流：请选择一张手牌，与 %src 交换",
  ["@@emo__jiaoliu-inhand"] = "交流",
}

skel:addEffect("active", {
  anim_type = "drawcard",
  prompt = "#emo__jiaoliu-prompt",
  card_num = 1,
  target_num = 1,
  card_filter = function (self, player, to_select, selected)
    return #selected == 0 and table.contains(player.player_cards[Player.Hand], to_select)
  end,
  target_filter = function (self, player, to_select, selected, selected_cards)
    return #selected == 0 and player ~= to_select and #selected_cards == 1 and not to_select:isKongcheng()
  end,
  times = function (self, player)
    return 1 - player:usedSkillTimes(skel.name, Player.HistoryPhase)
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(skel.name, Player.HistoryPhase) == 0
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local to = effect.tos[1]
    local to_cards = to:getCardIds("h")
    if #to_cards > 1 then
      to_cards = room:askToCards(to, { min_num = 1, max_num = 1, include_equip = false,
      skill_name = skel.name, cancelable = false, prompt = "#emo__jiaoliu-card:"..player.id})
    end
    local get = to_cards[1]
    local draw = not Fk:getCardById(get).is_damage_card
    room:swapCards(player, {{player, effect.cards}, {to, to_cards}}, skel.name)
    if table.contains(player.player_cards[Player.Hand], get) then
      room:setCardMark(Fk:getCardById(get), "@@emo__jiaoliu-inhand", 1)
    end
    if draw then
      if not player.dead then
        player:drawCards(1, skel.name)
      end
      if not to.dead then
        to:drawCards(1, skel.name)
      end
    end
  end,
})

skel:addEffect(fk.Damaged, {
  anim_type = "masochism",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and not player:isKongcheng()
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room:getOtherPlayers(player, false), function (p) return not p:isKongcheng() end)
    local tos, cards = room:askToChooseCardsAndPlayers(player, {
      min_card_num = 1, max_card_num = 1, max_num = 1, min_num = 1, targets = targets, skill_name = skel.name,
      pattern = ".|.|.|hand", prompt = "#emo__jiaoliu-prompt",
    })
    if #tos > 0 and #cards > 0 then
      event:setCostData(self, { tos = tos, cards = cards })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local mainSkill = Fk.skills[skel.name]
    if mainSkill and mainSkill:isInstanceOf(ActiveSkill) then
      mainSkill:onUse(player.room, { from = player, tos = event:getCostData(self).tos, cards = event:getCostData(self).cards })
    end
  end,
})

skel:addEffect(fk.PreCardUse, {
  can_refresh = function (self, event, target, player, data)
    return target == player and data.card.is_damage_card
  end,
  on_refresh = function (self, event, target, player, data)
    for _, id in ipairs(Card:getIdList(data.card)) do
      if Fk:getCardById(id).is_damage_card and Fk:getCardById(id):getMark("@@emo__jiaoliu-inhand") ~= 0 then
        data.additionalDamage = (data.additionalDamage or 0) + 1
      end
    end
  end,
})

skel:addEffect("maxcards", {
  exclude_from = function(self, player, card)
    return card and card:getMark("@@emo__jiaoliu-inhand") ~= 0
  end,
})

return skel
